<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Widget Overview</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="GTK+ 2.0 Tutorial"
HREF="book1.html"><LINK
REL="PREVIOUS"
TITLE="Table Packing Example"
HREF="x442.html"><LINK
REL="NEXT"
TITLE="Widget Hierarchy"
HREF="x479.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>GTK+ 2.0 Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x442.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x479.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="CH-WIDGETOVERVIEW"
></A
>Widget Overview</H1
><P
>The general steps to creating a widget in GTK are:</P
><P
></P
><OL
TYPE="1"
><LI
><P
> gtk_*_new() - one of various functions to create a new widget.
These are all detailed in this section.</P
></LI
><LI
><P
> Connect all signals and events we wish to use to the
appropriate handlers.</P
></LI
><LI
><P
> Set the attributes of the widget.</P
></LI
><LI
><P
> Pack the widget into a container using the appropriate call
such as gtk_container_add() or gtk_box_pack_start().</P
></LI
><LI
><P
> gtk_widget_show() the widget.</P
></LI
></OL
><P
>gtk_widget_show() lets GTK know that we are done setting the
attributes of the widget, and it is ready to be displayed. You may
also use gtk_widget_hide to make it disappear again. The order in
which you show the widgets is not important, but I suggest showing the
window last so the whole window pops up at once rather than seeing the
individual widgets come up on the screen as they're formed. The
children of a widget (a window is a widget too) will not be displayed
until the window itself is shown using the gtk_widget_show() function.</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="SEC-CASTING"
>Casting</A
></H1
><P
>You'll notice as you go on that GTK uses a type casting system. This
is always done using macros that both test the ability to cast the
given item, and perform the cast. Some common ones you will see are:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>  G_OBJECT (object)
  GTK_WIDGET (widget)
  GTK_OBJECT (object)
  GTK_SIGNAL_FUNC (function)
  GTK_CONTAINER (container)
  GTK_WINDOW (window)
  GTK_BOX (box)</PRE
></TD
></TR
></TABLE
><P
>These are all used to cast arguments in functions. You'll see them in the
examples, and can usually tell when to use them simply by looking at the
function's declaration.</P
><P
>As you can see below in the class hierarchy, all GtkWidgets are
derived from the GObject base class. This means you can use a widget
in any place the function asks for an object - simply use the
<TT
CLASS="LITERAL"
>G_OBJECT()</TT
> macro.</P
><P
>For example:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>g_signal_connect( G_OBJECT (button), "clicked",
                  G_CALLBACK (callback_function), callback_data);</PRE
></TD
></TR
></TABLE
><P
>This casts the button into an object, and provides a cast for the
function pointer to the callback.</P
><P
>Many widgets are also containers. If you look in the class hierarchy
below, you'll notice that many widgets derive from the Container
class. Any one of these widgets may be used with the
<TT
CLASS="LITERAL"
>GTK_CONTAINER</TT
> macro to pass them to functions that ask for
containers.</P
><P
>Unfortunately, these macros are not extensively covered in the
tutorial, but I recommend taking a look through the GTK header
files or the GTK API reference manual. It can be very educational. In fact, 
it's not difficult to learn how a widget works just by looking at the 
function declarations.</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x442.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x479.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Table Packing Example</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Widget Hierarchy</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>